package com.block.source.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.exception.NotifyExceptionService;
import com.block.db.entity.SourceMemberDataInfoHistory;
import com.block.db.entity.SourceMemberDataInfoTemp;
import com.block.db.mapper.SourceMemberDataInfoHistroyMapper;
import com.block.source.service.ISourceMemberDataInfoHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


@Service
public class SourceMemberDataInfoHistroyServiceImpl extends ServiceImpl<SourceMemberDataInfoHistroyMapper, SourceMemberDataInfoHistory> implements ISourceMemberDataInfoHistoryService {


    @Autowired
    NotifyExceptionService notifyExceptionService;
    @Autowired
    SourceMemberDataInfoHistroyMapper sourceMemberDataInfoHistroyMapper;

    @Async
    @Override
    public void saveMemberData(SourceMemberDataInfoTemp userDataParam) {
        try {
            String mobileMd5 = userDataParam.getMobileMd5();

            int mobileMd5Exist = sourceMemberDataInfoHistroyMapper.isMobileMd5Exist(mobileMd5);

            if (mobileMd5Exist == 0) {
                SourceMemberDataInfoHistory historyDataInfo = new SourceMemberDataInfoHistory();
                BeanUtil.copyProperties(userDataParam, historyDataInfo, true);

                boolean save = this.save(historyDataInfo);

                if (!save) {
                    notifyExceptionService.notifyNoticeInfo("MERCHANT_HISTORY_SAVE_EXEC",
                            "撞库历史表保存失败", "保存结果失败,ID:" + userDataParam.getId(), new Exception("保存失败"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            notifyExceptionService.notifyNoticeInfo("MERCHANT_HISTORY_SAVE_EXEC",
                    "撞库历史表保存失败", "保存失败", e);
        }
    }

}

